<template>
  <a-spin :spinning="uploading" tip="上传中...">
    <div class="uplaodBoxB page-miH page-bgBack">
      <a-row class="head">
        <div style="cursor: pointer" @click="isGoBack">
          <a-icon type="left" />
          <div class="left2">返回</div>
        </div>
        <a-col class="title">
          <strong>批量导入新员工</strong>
        </a-col>
      </a-row>
      <div class="ant-upload-select-text-box" style="padding-top: 24px" v-if="!callback">
        <a-upload ref="upload" :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload">
          <img :src="uploadImg" style="width: 139px; height: 106px" />
          <div class="ant-modal-body">
            <div style="font-size: 12px; color: #666">点击添加文件或将文件拖拽到此处</div>
            <div style="font-size: 12px; color: #999">(文件大小不超过5M)</div>
          </div>
          <a-button style="margin-bottom: 15px" type="primary"><a-icon type="plus" />添加文件</a-button>
        </a-upload>
      </div>
      <div class="tipBox" v-if="!callback">
        <a-row style="color: #252525; font-size: 16px; font-weight: 500"> 温馨提示 </a-row>
        <a-row>
          <a-col :span="12">
            <span style="padding-right: 5px"> 首先下载标准模板，填写信息后再上传 </span>
            <a-button type="link" @click="download" class="primary_color" style="font-size: 12px">点击下载模板</a-button>
            <!-- <div>1.支持WPS、excel2003以上版本文件</div> -->
            <div class="rowMargin">1.请将要上传的内容放在模板的员工数据中,单次导入员工上限为2000条</div>
            <div class="rowMargin">2.姓名、身份证号、手机号、工作性质、入职日期为必填项，顺序可调整，但不可删除</div>
            <div class="rowMargin">3.若字段为下拉选项，单元格的内容只能在支持的选项值内；不在范围内的单元格内容无法导入</div>
            <div class="rowMargin">4.若Excel文件设有权限密码，需取消Excel文件密码，否则系统无法识别</div>
            <div class="rowMargin">5.已添加到系统中的自定义字段，可实现批量导入相关信息（需手动添加表头）</div>
          </a-col>
        </a-row>
      </div>
      <div v-if="callback">
        <div style="padding-left: 24px; padding-right: 24px">
          <div
            style="
              margin-top: 15px;
              padding-left: 10px;
              display: flex;
              flex-flow: row;
              align-items: center;
              height: 35px;
              width: 100%;
              background-color: #fffbe6;
              border-style: solid;
              border-color: #ffe58f;
              border-width: 1px;
              border-radius: 5px;
            "
          >
            <img style="width: 16px; height: 16px; margin-right: 5px" src="../../../assets/icons/重复.png" />
            <span sytle="color:#55565D;font-size:14px;"
              >共{{ count }}条数据，成功导入{{ succ }}条数据，<label class="redc">{{ fial }}</label
              >条数据导入失败。</span
            >
          </div>
        </div>
        <div class="header-title-box">
          <div class="headerTitle">
            <div class="block" />
            <div style="font-size: 15px; font-weight: 700; font-color: #252525">错误信息</div>
          </div>
          <div class="btn-box">
            <a-button :disabled="!downloadurl" type="primary" @click="buCha"> 导出错误数据 </a-button>
          </div>
        </div>
        <a-table
          style="padding-left: 24px; padding-right: 24px"
          :columns="columnDetail"
          :pagination="pagination"
          :data-source="dataDetail"
          :scroll="{ x: 800 }"
          @change="handleTableChange"
        >
          <template slot="reason" slot-scope="text">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ text }}</span>
              </template>
              <span style="color: #ff0000">{{ text }}</span>
            </a-tooltip>
          </template>
          <template slot="name" slot-scope="text, record">
            <span v-if="record.field === 'name'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="idCard" slot-scope="text, record">
            <span v-if="record.field === 'idCard'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="phone" slot-scope="text, record">
            <span v-if="record.field === 'phone'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="postType" slot-scope="text, record">
            <span v-if="record.field === 'postType'" style="color: #ff0000">{{
              record.postType != null ? $store.state.enums.postTypeArr[record.postType - 1] : ""
            }}</span>
            <span v-else>{{ record.postType != null ? $store.state.enums.postTypeArr[record.postType - 1] : "" }}</span>
          </template>
          <template slot="joinDate" slot-scope="text, record">
            <span v-if="record.field === 'joinDate'" style="color: #ff0000">{{
              record.joinDate ? moment(text).format("YYYY-MM-DD") : ""
            }}</span>
            <span v-else>{{ record.joinDate ? moment(text).format("YYYY-MM-DD") : "" }}</span>
          </template>
          <template slot="turnStraightDate" slot-scope="text, record">
            <span v-if="record.field === 'turnStraightDate'" style="color: #ff0000">{{
              record.turnStraightDate ? moment(text).format("YYYY-MM-DD") : ""
            }}</span>
            <span v-else>{{ record.turnStraightDate ? moment(text).format("YYYY-MM-DD") : "" }}</span>
          </template>
          <template slot="formalType" slot-scope="text, record">
            <span v-if="record.field === 'formalType'" style="color: #ff0000">{{
              record.formalType ? formalType[Number(text - 1)] : ""
            }}</span>
            <span v-else>{{ record.formalType ? formalType[Number(text - 1)] : "" }}</span>
          </template>
          <template slot="probationDays" slot-scope="text, record">
            <span v-if="record.field === 'probationDays'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="nation" slot-scope="text, record">
            <span v-if="record.field === 'nation'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="nativeLocation" slot-scope="text, record">
            <span v-if="record.field === 'nativeLocation'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="maritalStatus" slot-scope="text, record">
            <span v-if="record.field === 'maritalStatus'" style="color: #ff0000">{{
              record.maritalStatus ? getMaritalStatus(record.maritalStatus) : ""
            }}</span>
            <span v-else>{{ record.maritalStatus ? getMaritalStatus(record.maritalStatus) : "" }}</span>
          </template>
          <template slot="joinWorkDate" slot-scope="text, record">
            <span v-if="record.field === 'joinWorkDate'" style="color: #ff0000">{{
              record.joinWorkDate ? moment(text).format("YYYY-MM-DD") : ""
            }}</span>
            <span v-else>{{ record.joinWorkDate ? moment(text).format("YYYY-MM-DD") : "" }}</span>
          </template>
          <template slot="regType" slot-scope="text, record">
            <span v-if="record.field === 'regType'" style="color: #ff0000">{{ record.regType ? getRegType(record.regType) : "" }}</span>
            <span v-else>{{ record.regType ? getRegType(record.regType) : "" }}</span>
          </template>
          <template slot="currentLocation" slot-scope="text, record">
            <span v-if="record.field === 'currentLocation'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="politicalStatus" slot-scope="text, record">
            <span v-if="record.field === 'politicalStatus'" style="color: #ff0000">{{
              record.politicalStatus ? renderPoliticalStatus(record.politicalStatus) : ""
            }}</span>
            <span v-else>{{ record.politicalStatus ? renderPoliticalStatus(record.politicalStatus) : "" }}</span>
          </template>
          <template slot="contractStartDate" slot-scope="text, record">
            <span v-if="record.field === 'contractStartDate'" style="color: #ff0000">{{
              record.contractStartDate ? moment(text).format("YYYY-MM-DD") : ""
            }}</span>
            <span v-else>{{ record.contractStartDate ? moment(text).format("YYYY-MM-DD") : "" }}</span>
          </template>
          <template slot="contractEndDate" slot-scope="text, record">
            <span v-if="record.field === 'contractEndDate'" style="color: #ff0000">{{
              record.contractEndDate ? moment(text).format("YYYY-MM-DD") : ""
            }}</span>
            <span v-else>{{ record.contractEndDate ? moment(text).format("YYYY-MM-DD") : "" }}</span>
          </template>
          <template slot="contractType" slot-scope="text, record">
            <span v-if="record.field === 'contractType'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="contactsTerm" slot-scope="text, record">
            <span v-if="record.field === 'reacontactsTermson'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="channelName" slot-scope="text, record">
            <span v-if="record.field === 'channelName'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="bankName" slot-scope="text, record">
            <span v-if="record.field === 'bankName'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="bankCardBranch" slot-scope="text, record">
            <span v-if="record.field === 'bankCardBranch'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="bankCard" slot-scope="text, record">
            <span v-if="record.field === 'bankCard'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="education" slot-scope="text, record">
            <span v-if="record.field === 'education'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="pfCard" slot-scope="text, record">
            <span v-if="record.field === 'pfCard'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="remark" slot-scope="text, record">
            <span v-if="record.field === 'remark'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="tag" slot-scope="text, record">
            <span v-if="record.field === 'tag'" style="color: #ff0000">{{ text }}</span>
            <span v-else>{{ text }}</span>
          </template>
        </a-table>
      </div>

      <a-modal v-model="dialogFailVisible" title="批量导入" width="500px" @cancel="handleFailClose" @ok="handleFailClose">
        <template slot="footer">
          <a-button key="submit" type="primary" @click="handleFailClose"> 确定 </a-button>
        </template>
        <div style="padding-left: 10px; display: flex; flex-flow: row; align-items: center; height: 35px; width: 100%">
          <img style="width: 25px; height: 25px; margin-right: 10px" src="../../../assets/icons/duihao.png" />
          <div style="color: #3c3d43; font-size: 15px; font-weight: 700">共{{ count }}条数据，开始导入，请在右下角任务列表查看进度</div>
        </div>
      </a-modal>
    </div>
  </a-spin>
</template>
<script>
import UploadMixin from "xqjr-plugin-common/UploadOrExport/uploadMixin";
const mixin = new UploadMixin();
const formalType = ["试用", "正式", "实习"];
const columnDetail = [
  {
    title: "错误信息",
    dataIndex: "reason",
    fixed: "left",
    scopedSlots: { customRender: "reason" },
    ellipsis: true,
    width: 260
  }
];
import { renderPoliticalStatus } from "@/views/staff/management/employeeFuncs";
import { mapGetters } from "vuex";
export default {
  mixins: [mixin],
  data() {
    return {
      count: 0,
      succ: 0,
      fial: 0,
      formalType,
      pagination: {
        pageSize: 10,
        total: 0,
        current: 1,
        showQuickJumper: true
      },
      dataDetail: [],
      callback: false,
      dialogFailVisible: false,
      downloadurl: undefined,
      columnDetail,
      uploadImg: require("@/assets/img/uploadImg.png"),
      url: `/hr/api/v1/excel/template/download`,
      excelName: "花名册批量导入模板.xlsx"
    };
  },
  computed: {
    ...mapGetters(["isJF"]),
    params() {
      return {
        templateName: this.isJF ? "employeeRosterTemplateZhiXinRenJiaFang.xlsx" : "employeeRosterTemplateZhiXinRen.xlsx"
      };
    }
  },
  created() {
    const companyId = this.$store.state.hr.companyId;
    this.action = `/hr/api/v1/empInfos/importZhiXinRen?companyId=${companyId}&force=false`;
  },
  methods: {
    handleSuccess(res) {
      var that = this;
      if (res.success && res.data !== null) {
        var numAll = res.data.importFailList.length + res.data.importSucList.length;
        var numFail = res.data.importFailList.length;
        const numSuc = res.data.importSucList.length;
        if (that.showFaild) {
          that.Faildata = res.data.importFailList;
          that.Faildata.forEach((item, index) => {
            item = Object.assign(item, item.allMap);
          });
          that.showFaild(numAll, numSuc, numFail, that.Faildata, res.data.excelFilePath, res.data.failHeaders);
        } else {
          if (numFail === 0) {
            this.$confirm({
              okText: "确定",
              title: `共导入${numAll}人，${numSuc}人成功，${numFail}人未成功`,
              onOk() {
                that.goback();
              },
              onCancel() {
                that.goback();
              }
            });
          } else {
            this.$confirm({
              okText: "确定",
              cancelText: "查看未成功名单",
              title: `共导入${numAll}人，${numSuc}人成功，${numFail}人未成功`,
              onOk() {
                that.goback();
              },
              onCancel() {
                that.Faildata = res.data.importFailList;
                that.dialogFailVisible = true;
              }
            });
          }
        }
      } else if (res.data === null) {
        this.$confirm({
          okText: "确定",
          title: res.msg
        });
      } else {
        this.$notification["error"]({
          message: "error",
          description: res.msg || "上传失败"
        });
        setTimeout(() => {
          this.reset();
        }, 2000);
      }
    },
    renderPoliticalStatus,
    getRegType(regType) {
      if (regType === 1) {
        return "城镇户口";
      } else if (regType === 2) {
        return "农村户口";
      } else if (regType === 3) {
        return "居民户口";
      }
    },
    buCha() {
      const token = this.$store.state.auth.token;
      window.open(this.downloadurl + `?token=${token}`);
    },
    isGoBack() {
      if (this.callback) {
        this.callback = false;
      } else {
        this.goback();
      }
    },
    handleUploadError() {
      this.$message.warning("没有文件");
    },
    handleFailClose() {
      this.dialogFailVisible = false;
      this.uploading = false;
      this.goback();
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination.current = pagination.current;
    },
    showFaild(numAll, numSuc, numFail, Faildata, downloadurl, columns) {
      this.columnDetail = [
        {
          title: "错误信息",
          dataIndex: "reason",
          fixed: "left",
          scopedSlots: { customRender: "reason" },
          ellipsis: true,
          width: 260
        }
      ];
      this.count = numAll;
      this.succ = numSuc;
      this.fial = numFail;
      this.dataDetail = Faildata;
      if (this.count === this.succ) {
        this.dialogFailVisible = true;
      } else {
        this.callback = true;
      }
      this.downloadurl = downloadurl;
      columns.forEach((item, index) => {
        this.columnDetail.push({
          title: `${item}`,
          dataIndex: `${item}`,
          align: "center",
          width: item === "身份证号" ? 220 : 150,
          scopedSlots: { customRender: `${item}` },
          ellipsis: true
          // fixed: item === "身份证号" || item === "姓名" ? "left" : null
        });
      });
    },
    getMaritalStatus(maritalStatus) {
      if (maritalStatus === 1) {
        return "已婚";
      } else if (maritalStatus === 2) {
        return "未婚";
      } else if (maritalStatus === 3) {
        return "离异";
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.header-title-box {
  padding-left: 24px;
  padding-right: 24px;
  margin-top: 10px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .headerTitle {
    display: flex;
    justify-content: flex-start;
    .block {
      width: 3px;
      height: 20px;
      margin-top: 2px;
      background-color: var(--primary);
      margin-right: 12px;
    }
  }
}
.uplaodBoxB {
  padding-bottom: 24px;
  .head {
    background-color: #fff;
    .anticon-left {
      z-index: 3;
      left: 24px;
      position: absolute;
      color: #595959;
      font-size: 20px;
      margin-top: 21px;
      color: #c0c0c0;
    }
    .left2 {
      z-index: 3;
      left: 45px;
      position: absolute;
      font-size: 14px;
      cursor: pointer;
      color: #c0c0c0;
    }
    font-size: 16px;
    color: #000;
    font-weight: 500;
    text-align: center;
    line-height: 60px;
    height: 60px;
    border-bottom: 1px solid #e8e8e8;
  }
  .exportTitle {
    text-align: center;
    font-size: 16px;
    font-weight: 500;
    color: #252525;
  }
  .left2 {
    z-index: 3;
    left: 50px;
    position: absolute;
    color: #595959;
    font-size: 14px;
  }
  .anticon-left {
    z-index: 3;
    left: 24px;
    position: absolute;
    color: #d8d8d8;
    font-size: 26px;
    margin-top: 17px;
  }
  .tipBox {
    line-height: 18px;
    background: #faf9f9;
    border-radius: 4px;
    font-size: 12px;
    color: #595959;
    margin: 0 24px;
    padding: 12px;
    height: 185px;
  }
  .rowMargin {
    margin-top: 4px;
  }
  .ant-upload-select-text-box {
    border: 1px dashed #b8b8b8;
    background: #fff;
    margin: 24px;
    text-align: center;
    cursor: pointer;
    border-radius: 4px;
    padding: 12px;
  }
  .ant-upload-select-text-box ::v-deep .ant-upload-list {
    display: none;
  }
  .ant-upload-list-item-name {
    text-align-last: left;
  }

  .anticon-upload {
    font-size: 40px;
  }

  .ant-modal-body {
    line-height: 20px;

    em {
      color: var(--primary);
      font-style: normal;
    }
  }
}
.redc {
  color: red;
}
</style>
